package com.mdsoft.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.mdsoft.po.Choose;
import com.mdsoft.po.User;
import com.mdsoft.service.ChooseService;
import com.mdsoft.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport implements SessionAware, RequestAware{
	private static final long serialVersionUID = -2102203506483530212L;

	private UserService userService;
	
	private ChooseService chooseService;

	private String username;

	private String password;

	private Map<String, Object> session;

	private Map<String, Object> request;
	
	private int[] userid;
	
	private float[] userpower;
	
	public String login() {
		
		User user = userService.login(username, password);
		if (user != null) {
			session.put("user", user);
		}
		List<User> users = userService.getAll();
		users.remove(user);
		request.put("users", users);
		return SUCCESS;
	}
	
	public String select () {
		List<User> users = new ArrayList<User>();
		for (int id : userid) {
			users.add(userService.get(id));
		}
		
		request.put("users", users);
		return SUCCESS;
	}

	public String power() {
		User fromUser = (User) session.get("user");
		for (int i = 0; i < userid.length; i++) {
			User toUser = userService.get(userid[i]);
			
			Choose choose = new Choose();
			choose.setFromUser(fromUser);
			choose.setToUser(toUser);
			choose.setPower(userpower[i]);
			
			chooseService.add(choose);
		}
		return SUCCESS;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ChooseService getChooseService() {
		return chooseService;
	}

	public void setChooseService(ChooseService chooseService) {
		this.chooseService = chooseService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int[] getUserid() {
		return userid;
	}

	public void setUserid(int[] userid) {
		this.userid = userid;
	}

	public float[] getUserpower() {
		return userpower;
	}

	public void setUserpower(float[] userpower) {
		this.userpower = userpower;
	}
	
	
}
